<template>
  <div class="complaint-detail-bg">
    <el-card class="complaint-detail-card">
      <div class="complaint-detail-header">
        <el-button @click="goBack" icon="ArrowLeft">返回</el-button>
        <div class="complaint-detail-title">投诉详情</div>
      </div>
      
      <div v-if="loading" class="loading-container">
        <el-loading-spinner />
        <p>加载中...</p>
      </div>
      
      <div v-else-if="complaint.id">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="投诉编号">{{ complaint.id }}</el-descriptions-item>
          <el-descriptions-item label="订单编号">{{ orderNumber }}</el-descriptions-item>
          <el-descriptions-item label="投诉时间">{{ formatTime(complaint.complaint_time) }}</el-descriptions-item>
          <el-descriptions-item label="处理状态">
            <el-tag :type="getStatusType(complaint.status)">
              {{ formatStatus(complaint.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="投诉内容">
            <div class="complaint-content">{{ complaint.content }}</div>
          </el-descriptions-item>
          <el-descriptions-item label="处理结果" v-if="complaint.result">
            <div class="complaint-result">{{ complaint.result }}</div>
          </el-descriptions-item>
        </el-descriptions>
        
        <div v-if="complaint.status === 0" class="complaint-actions">
          <el-button type="danger" @click="cancelComplaint">撤销投诉</el-button>
        </div>
      </div>
      
      <div v-else class="no-data">
        <el-empty description="未找到投诉信息" />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { orderComplaintAPI, orderAPI } from '@/api/order'

const route = useRoute()
const router = useRouter()
const complaint = ref({})
const orderNumber = ref('')
const loading = ref(true)

const complaintId = route.params.id

function formatStatus(status) {
  const map = {
    0: '待处理',
    1: '处理中', 
    2: '已解决'
  }
  return map[status] || '未知'
}

function getStatusType(status) {
  const map = {
    0: 'warning',
    1: 'primary',
    2: 'success'
  }
  return map[status] || 'info'
}

function formatTime(timeStr) {
  if (!timeStr) return '--'
  return new Date(timeStr).toLocaleString('zh-CN')
}

async function fetchComplaintDetail() {
  try {
    loading.value = true
    const res = await orderComplaintAPI.getComplaintDetail(complaintId)
    complaint.value = res.data
    
    // 获取订单编号
    if (complaint.value.order) {
      try {
        const orderRes = await orderAPI.getOrderDetail(complaint.value.order)
        orderNumber.value = orderRes.data.order_number || `ODR${String(complaint.value.order).padStart(7, '0')}`
      } catch (error) {
        // 如果获取订单详情失败，生成标准格式的订单编号
        orderNumber.value = `ODR${String(complaint.value.order).padStart(7, '0')}`
      }
    }
  } catch (error) {
    console.error('获取投诉详情失败:', error)
    ElMessage.error('获取投诉详情失败')
  } finally {
    loading.value = false
  }
}

async function cancelComplaint() {
  try {
    await ElMessageBox.confirm('确定要撤销投诉吗？', '确认撤销', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    await orderComplaintAPI.deleteComplaint(complaintId)
    ElMessage.success('投诉已撤销')
    router.go(-1)
  } catch (error) {
    if (error !== 'cancel') {
      console.error('撤销投诉失败:', error)
      ElMessage.error('撤销投诉失败')
    }
  }
}

function goBack() {
  router.go(-1)
}

onMounted(() => {
  fetchComplaintDetail()
})
</script>

<style scoped>
.complaint-detail-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #fff 60%, #e6f7ff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.complaint-detail-card {
  width: 100%;
  max-width: 600px;
  border-radius: 18px;
  box-shadow: 0 4px 24px 0 rgba(34,34,34,0.08);
  padding: 32px 36px 24px 36px;
  position: relative;
}

.complaint-detail-header {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
}

.complaint-detail-title {
  font-size: 26px;
  font-weight: bold;
  color: #222;
  text-align: center;
  margin: 0 auto;
  letter-spacing: 2px;
}

.loading-container {
  text-align: center;
  padding: 40px 0;
}

.complaint-content,
.complaint-result {
  line-height: 1.6;
  padding: 8px 0;
  white-space: pre-wrap;
}

.complaint-actions {
  margin-top: 24px;
  text-align: center;
}

.no-data {
  padding: 40px 0;
}

/* 美化描述列表 */
:deep(.el-descriptions__label) {
  font-weight: 600;
  color: #334155;
  width: 120px;
}

:deep(.el-descriptions__content) {
  color: #475569;
}

/* 美化按钮 */
:deep(.el-button) {
  border-radius: 12px;
  font-weight: 600;
  transition: all 0.3s ease;
  padding: 12px 24px;
}

:deep(.el-button--danger) {
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(239, 68, 68, 0.3);
}

:deep(.el-button--danger:hover) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(239, 68, 68, 0.4);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .complaint-detail-bg {
    padding: 16px;
  }
  
  .complaint-detail-card {
    padding: 24px;
    border-radius: 16px;
  }
  
  .complaint-detail-title {
    font-size: 24px;
  }
}
</style>
